package com.nantai.jiafang.nantaijiafang.zhijian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nantai.jiafang.nantaijiafang.Data;
import com.nantai.jiafang.nantaijiafang.IConstant;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.ViewModel.Zhijian_Net;
import com.nantai.jiafang.nantaijiafang.activity.adapter.SearchDateAdapter;
import com.nantai.jiafang.nantaijiafang.activity.adapter.SewingDataAdapter;
import com.nantai.jiafang.nantaijiafang.base.BaseActivity;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.SewingDataRes;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.SewingDetail;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Tip;
import com.nantai.jiafang.nantaijiafang.databinding.ActivitySewingDetailBinding;
import com.nantai.jiafang.nantaijiafang.databinding.ActivitySewingSearchBinding;
import com.nantai.jiafang.nantaijiafang.databinding.ActivityZhiJianBinding;
import com.nantai.jiafang.nantaijiafang.view.XListView;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.SewingAddFragment;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.ZhijianFragment_add;
import com.zjj.spinnerlibrary.model.SpinnerModel;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SewingActivity extends BaseActivity<ActivitySewingSearchBinding> implements View.OnClickListener, XListView.IXListViewListener {


    private ActivitySewingSearchBinding binding;

    private int type;

    private EditText inspectorInfo,etProductInfo;

    private XListView xListView;

    private Button btnAdd,btdSearch;

    private TextView tvBack;

    private Gson gson = new Gson();

    private SewingDataAdapter sewingDataAdapter;

    private int page = 1;

    private int pages = 1;

    private int pageSize = 10;

    private Handler mHandler;

    @Override
    public int layoutid() {
        return R.layout.activity_sewing_search;
    }

    @Override
    public void init(ActivitySewingSearchBinding binding) {
        this.binding=binding;

        type=getIntent().getIntExtra("type",0);

        initView();
        binding.setOnclicklistener(this);
        mHandler = new Handler();
        // loading data
        getData();
        binding.inspectorInfo.setText(Data.ZHIJIAN_Account);

    }

    @Override
    protected void onResume() {
        if(sewingDataAdapter != null){
            getQuiltInsHead(0);
        }
        super.onResume();
    }

    private void initView(){
        inspectorInfo = (EditText) findViewById(R.id.inspectorInfo);

        etProductInfo = (EditText) findViewById(R.id.productInfo);

        xListView = (XListView) findViewById(R.id.xListView);
        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);
        xListView.setXListViewListener(this);

        btnAdd = (Button) findViewById(R.id.btn_add);//绗缝巡检 新增

        btdSearch = (Button) findViewById(R.id.btn_search);

        tvBack = (TextView) findViewById(R.id.tv_back);
    }

    private void getData(){
        getQuiltInsHead(0);
    }


    private void getQuiltInsHead(int dataType){
        Map<String,String> map = new HashMap<>();

        map.put("ProductInfo",etProductInfo.getText().toString());
        map.put("InspectorInfo",inspectorInfo.getText().toString());
        map.put("PageIndex",page+"");
        map.put("PageCount",pageSize+"");
        map.put("loginName",Data.NTLogin_Account);
        if(type == 0){
            Zhijian_Net.getQuiltInsHead(getApplicationContext(), map, new Zhijian_Net.GetSingData() {
                @Override
                public void success(Object s) {

                    SewingDataRes sewingDataRes = (SewingDataRes) s;

                    List<Map<String,Integer>> countNos = sewingDataRes.getCountNo();

                    if(countNos!= null&& countNos.size() >0 ){
                        int totals = countNos.get(0).get("TotalCount");

                        if(totals % pageSize == 0){
                            pages = totals/pageSize;
                        }else{
                            pages = totals/pageSize + 1;
                        }
                    }


                    List<SewingDetail> list = sewingDataRes.getQuiltInspect();

                    if(sewingDataAdapter == null){
                        sewingDataAdapter = new SewingDataAdapter(getApplicationContext(), list, new SewingDataAdapter.Callback() {
                            @Override
                            public void delete(String guid) {
                                Map<String,String> map = new HashMap<>();
                                map.put("Guid",guid);

                                Zhijian_Net.deleteQuiltHead(getApplicationContext(), map, new Zhijian_Net.GetSingData<List<Tip>>() {
                                    @Override
                                    public void success(List<Tip> s) {
                                        sewingDataAdapter.notifyDataSetChanged();
                                    }
                                });
                            }

                            @Override
                            public void detail(SewingDetail detail) {
                                SewingAddFragment addFragment = new SewingAddFragment();
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("detail",detail);
                                addFragment.setArguments(bundle);
                                getSupportFragmentManager().beginTransaction()
                                        .addToBackStack(null)
                                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                                        .replace(R.id.container3,addFragment,"SewingAddFragment")
                                        .commit();
                            }
                        });
                        xListView.setAdapter(sewingDataAdapter);
                    }else{
                        if(dataType ==0){
                            sewingDataAdapter.refreshList(list);
                        }else{
                            sewingDataAdapter.setList(list);
                        }

                    }


                }
            });
        }else{
            Zhijian_Net.getCutInsHead(getApplicationContext(), map, new Zhijian_Net.GetSingData() {
                @Override
                public void success(Object s) {

                    SewingDataRes sewingDataRes = (SewingDataRes) s;

                    List<Map<String,Integer>> countNos = sewingDataRes.getCountNo();

                    if(countNos!= null&& countNos.size() >0 ){
                        int totals = countNos.get(0).get("TotalCount");

                        if(totals % pageSize == 0){
                            pages = totals/pageSize;
                        }else{
                            pages = totals/pageSize + 1;
                        }
                    }


                    List<SewingDetail> list = sewingDataRes.getCutInspect();

                    if(sewingDataAdapter == null){
                        sewingDataAdapter = new SewingDataAdapter(getApplicationContext(), list, new SewingDataAdapter.Callback() {
                            @Override
                            public void delete(String guid) {
                                Map<String,String> map = new HashMap<>();
                                map.put("Guid",guid);

                                Zhijian_Net.deleteQuiltHead(getApplicationContext(), map, new Zhijian_Net.GetSingData<List<Tip>>() {
                                    @Override
                                    public void success(List<Tip> s) {
                                        sewingDataAdapter.notifyDataSetChanged();
                                    }
                                });
                            }

                            @Override
                            public void detail(SewingDetail detail) {
                                SewingAddFragment addFragment = new SewingAddFragment();
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("detail",detail);
                                addFragment.setArguments(bundle);
                                getSupportFragmentManager().beginTransaction()
                                        .addToBackStack(null)
                                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                                        .replace(R.id.container3,addFragment,"SewingAddFragment")
                                        .commit();
                            }
                        });
                        xListView.setAdapter(sewingDataAdapter);
                    }else{
                        if(dataType ==0){
                            sewingDataAdapter.refreshList(list);
                        }else{
                            sewingDataAdapter.setList(list);
                        }

                    }


                }
            });
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                getSupportFragmentManager().beginTransaction()
                        .addToBackStack(null)
                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                        .add(R.id.container3, new SewingAddFragment(),"SewingAddFragment")
                        .commit();
                break;
            case R.id.btn_search:
                getQuiltInsHead(type);
                break;
            case R.id.tv_back:
                break;
        }
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1 ;
                getQuiltInsHead(0);
                onLoad();
            }
        },500);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(page < pages){
                    page++ ;
                    getQuiltInsHead(1);
                }
                onLoad();
            }
        },500);
    }

    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime("刚刚");
    }


}

